package hdu.edu.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import hdu.edu.Dao.bookDao;
import hdu.edu.Model.Book;

@Controller
@RequestMapping("/book")
@Transactional
public class bookController {
	
	@Autowired
	private bookDao bookDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav=new ModelAndView("book/booklist");
		List<Book> books=bookDao.findAll();
		System.out.print(books.size());
		mav.addObject("books",books);
		return mav;
		
	}
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ModelAndView get(@PathVariable int id){
		ModelAndView mav=new ModelAndView("book/book");
		Book book=bookDao.findById(id);
		mav.addObject("books",book);
		return mav;
		
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("book/addbook");
		return mav;
		
	}
	@RequestMapping(value="/out",method=RequestMethod.GET)
	public ModelAndView out(){
		ModelAndView mav=new ModelAndView("redirect:/book");
		return mav;
		
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String title,
			@RequestParam String author,
			@RequestParam String brief){
		Book book=new Book();
		book.setAuthor(author);
		book.setBrief(brief);
		book.setTitle(title);
		bookDao.add(book);
		ModelAndView mav=new ModelAndView("redirect:/book");
	

		return mav;
		
	}
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(@RequestParam int id){
		bookDao.delete(id);
		ModelAndView mav=new ModelAndView("redirect:/book");
		return mav;
	}
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(@RequestParam int id){
		
     Book book=bookDao.findById(id);
      ModelAndView mav=new ModelAndView("book/updatebook");
      mav.addObject("book",book);
	return mav;
		
	}
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(@RequestParam int id,
			@RequestParam String title,
			@RequestParam String author,
			@RequestParam String brief){
		
     Book book=new Book();
     book.setId(id);
     book.setTitle(title);
     book.setAuthor(author);
     book.setBrief(brief);
      bookDao.update(book);
      ModelAndView mav=new ModelAndView("redirect:/book");
		return mav;	
	}
	
	
	

	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView search(@RequestParam String title){
		ModelAndView mav=new ModelAndView("book/s");
		Book books=bookDao.find(title);
		mav.addObject("books",books);
		return mav;
		
	}
	
	
	
	

}
